﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

public partial class Manage_ImageHandler : System.Web.UI.Page
{
    private static int nowpages;
    private int PageSize=50;
    private int PageCount;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (string.IsNullOrEmpty(Request.QueryString["ftb"]) || string.IsNullOrEmpty(Request.QueryString["path"]))
        {
            Response.Write("<script>window.opener=null;window.close();</script>");
        }
        this.Folder = Request.QueryString["path"];
        this.FTB = Request.QueryString["ftb"];
        if (!IsPostBack)
        {
            nowpages = 1;
            this.BingImg();
        }
    }
    private void BingImg()
    {
        FileInfo[] f = GetFiles.Get(Server.MapPath("~/" + this.Folder));
        if (f == null)
        {
            Response.Write("<script>window.opener=null;window.close();</script>");
        }
        ArrayList fp = new ArrayList();
        for (int i = PageSize * (nowpages - 1); (i < f.Length)&& (i<PageSize * nowpages); i++)
        {
            fp.Add(f[i]);
        }
        Image_list_rp.DataSource = fp ;
        Image_list_rp.DataBind();

        this.PageCount = (int)Math.Ceiling((double)f.Length / (double)PageSize);//计算一共有多少页

        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == this.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= this.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        nowpages_lb.Text = nowpages.ToString();
        pagecount_lb.Text = this.PageCount.ToString();
        tpagesizes.Text = this.PageSize.ToString();
        pagesum.Text = f.Length.ToString();
    }

    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.BingImg();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.BingImg();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.BingImg();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(pagecount_lb.Text);
        this.BingImg();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.BingImg();
    }
    
    protected void UpLoadPic_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(PicFileUpload.PostedFile.FileName))
        {
            string filepath = PicFileUpload.PostedFile.FileName;
            string fileextname = filepath.Substring(filepath.LastIndexOf(".") + 1);//扩展名
            if (!(fileextname.ToLower() == "jpg" || fileextname.ToLower() == "jpeg" || fileextname.ToLower() == "gif" || fileextname.ToLower() == "png" || fileextname.ToLower() == "bmp"))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('选择的文件不是图片格式');</script>");
                return;
            }
            string mpath = Server.MapPath("~/"+this.Folder+"/");//文件夹路径
            string mfilename = DateTime.Now.ToString("yyyyMMddHHmmss") +( new Random()).Next(1000,10000);
            try
            {
                PicFileUpload.PostedFile.SaveAs(mpath + mfilename + "." + fileextname);
                this.BingImg();
                //DataBd();
            }
            catch
            {
                
            }
        }
        
    }
    protected string Folder
    {
        get;
        set;
    }
    protected string FTB
    {
        get;
        set;
    }

}
